

var Wizard = compono.Class({
	name:"Wizard",
	action:false,
	request:false,
	construtor:function(url){
		this.submit.scope = this;
		this.action = url || false;
	},
	show:function(){
		
	},
	run:function(){
	
	},
	attach:function(form){
		if(form){
			form.wizard = this;
			this.action = this.action || form.getAttribute("action");
			$(form).event("submit",function(e){
				this.wizard.submit(this,e);
			})
		}
	},
	submit:function(form, e){
		var x, request, wizard=this;

		this.window = new compono.ui.Window();
		this.window.title = "Wizard";
		this.window.modal = true;
		
		request = new compono.Ajax.Request();
		request.returnType = "application/json";
		request.postBody = this.getFormData(form, e);
		request.onComplete = function()
		{
			if(this.response.redirectUrl)
			{
				var request = new compono.Ajax.Request();
				request.onComplete = function()
				{
					wizard.window.run();
					wizard.step(this.response);
					wizard.window.show();
				};
				request.run(this.response.redirectUrl)
			}
		};
		request.run(this.action);
		
		e.preventDefault();
		return false;
	},
	step:function(content){
		this.window.content(content);
		var f = $T1("form",this.window.html);
		if(f){
			f.wizard = this;
			$(f).event("submit",function(e){
				
				request = new compono.Ajax.Request();
				request.postBody = this.wizard.getFormData(this,e);
				request.wizard = this.wizard;
				request.onComplete = function()
				{
					this.wizard.step(this.response);
				};
				request.run(this.action);
				
				e.preventDefault();
				return false;
			})
		}
	},
	getFormData:function(form,e){
		var str=[];
		
		for( var i=0, o, l=form.elements.length; i<l; i++){	
			o = form.elements[i]
			if(!o.disabled){
				switch(o.nodeName.toLowerCase()){
					case "input":
						switch(o.type.toLowerCase()){
							case "radio":
							case "checkbox":
								if(o.checked){
									str.push(o.name + "=" + o.value);
								}
								break;
							case "button":
							case "submit":
								continue; break;
							default:
								str.push(o.name + "=" + o.value);
						}
						break;
					case "select":
						if(o.multiple){
							for(var z=0;z<o.options.length;z++){
								if(o.options[z].selected){
									str.push(o.name + "=" + o.options[z].value);
								}
							}
						}else{
							str.push(o.name + "=" + o.value);
						};
						break;
					case "button": 
						continue; break;
					default:
						str.push(o.name + "=" + o.value);
						break;
				}
			}
		};
		
		submit = e.explicitOriginalTarget || e.relatedTarget || document.activeElement || false;
		if(submit){
			str.push(submit.name + "=" + submit.value);
		}
		
		return str.join("&");
	}
})



compono.ready(function(){
	
	smtpWizard = new Wizard();
	smtpWizard.attach($T1("form"))
	smtpWizard.run();
	
})